package com.drought.rained.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.WindowManager;

import com.drought.rained.BuildConfig;
import com.drought.rained.LJApplication;
import com.drought.rained.R;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.business.ParamsFactoryLJ;
import com.drought.rained.cache.AcacheKeysLJ;
import com.drought.rained.cache.AcacheLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.network.RetrofitHelperLJ;
import com.drought.rained.response.AuthResponseLJ;
import com.drought.rained.utils.ToastUtilsLJ;

import java.util.Timer;
import java.util.TimerTask;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class SplashActivityLJ extends BaseTitleActivity {

    @Override
    protected void initViews() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 全屏显示
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    @Override
    protected void onUIReady() {
        getVerifyData();
    }

    @Override
    protected int getContentResId() {
        return R.layout.layout_of_splash;
    }

    @SuppressLint("CheckResult")
    public void getVerifyData() {
        getVerifyOb().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<AuthResponseLJ>() {
                    @Override
                    public void accept(AuthResponseLJ verifyDataResponse) throws Exception {
                        if (verifyDataResponse.getStatus() == ConstantLJ.LOGIN_TIMEOUT) { //token 会过期
                            UserInfoLJ.logout();
                        }
                        jumpToMain();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        // 异常操作
                        ToastUtilsLJ.showLong(SplashActivityLJ.this, R.string.appbar_scrolling_view_behavior);
                        UserInfoLJ.logout();
                        jumpToMain();
                    }
                });
    }

    private void jumpToMain() {
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Intent intent;
                String cache = AcacheLJ.get(LJApplication.getContext()).getAsString(AcacheKeysLJ.GRANT);
                int grant = Integer.valueOf(TextUtils.isEmpty(cache) ? "0" : cache);
                if (grant == BuildConfig.grant) {
                    intent = new Intent(SplashActivityLJ.this, MainActivityLJ.class);
                } else  {
                    intent = new Intent(SplashActivityLJ.this, StatementActivity.class);
                }
                SplashActivityLJ.this.startActivity(intent);
                overridePendingTransition(R.anim.anim_in_lj, R.anim.anim_out_lj);
                finish();
            }
        };

        timer.schedule(timerTask, 1500);
    }

    private Observable<AuthResponseLJ> getVerifyOb() {
        return RetrofitHelperLJ.getRetorfitHelper().getApiService()
                .getAuthStatus(UserInfoLJ.getToken(), ParamsFactoryLJ.getCommonParams(null));
    }
}
